﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabManager.UserControls.Tabs.Templates;
using DataConnector;
using Library;
using Equin.ApplicationFramework;
using System.Data.Entity;

namespace LabManager.UserControls.Tabs.Masterdata
{
    public partial class TabMachine : TabTemplate
    {
        private BindingListView<V_MachineList> bindingList;
        private BindingSource bindingSource = new BindingSource();
        private int selectedID = -1;

        public TabMachine()
        {
            InitializeComponent();
        }

        public override void Load()
        {
            SetTexts();
            LoadMachineTypes();
            LoadMachineList();
        }

        private void SetTexts()
        {
            //TODO
        }

        private void LoadMachineTypes()
        {
            var machineTypes = new BindingListView<MachineType>(DataConnectorLayer.Instance.R_MachineType.FindAll(x => x.MachineTypeID > 0).ToList<MachineType>());

            cbMachineType.DataSource = machineTypes;
            cbMachineType.ValueMember = "MachineTypeID";
            cbMachineType.DisplayMember = "Identification";
        }

        private void LoadMachineList()
        {
            // Get new entities from view
            DataConnectorLayer.Instance.R_MachineList = new Repository<V_MachineList, DbContext>(new DbContext(Helper.ENTITY_NAME));
            bindingList = new BindingListView<V_MachineList>(DataConnectorLayer.Instance.R_MachineList.FindAll(x => x.MachineID > 0).ToList<V_MachineList>());
            bindingSource.DataSource = bindingList;
            dgMain.DataSource = bindingSource;
            bnNavi.BindingSource = bindingSource;

            if (dgMain.Rows.Count > 0)
            {
                selectedID = (int)dgMain.Rows[0].Cells[0].Value;
            }

            dgMain_SelectionChanged(null, null);
        }

        #region Events

        private void dgMain_SelectionChanged(object sender, EventArgs e)
        {
            if (dgMain.SelectedRows.Count > 0)
            {
                var selectedRow = dgMain.SelectedRows[0];

                selectedID = (int)selectedRow.Cells[0].Value;
                tbNameText.Text = selectedRow.Cells[1].Value.ToString();
                cbMachineType.Text = selectedRow.Cells[2].Value.ToString();

                // Delete button
                var canDeleteMachine = DataConnectorLayer.Instance.R_Analysis.FindOne(x => x.Machine.MachineID == selectedID) == null;
                btDelete.Enabled = canDeleteMachine;
                btUpdate.Enabled = true;
            }
            else
            {
                tbNameText.Text = string.Empty;

                btUpdate.Enabled = false;
                btDelete.Enabled = false;
            }

            // New button
            var canCreateMachineType = DataConnectorLayer.Instance.R_MachineType.Set<MachineType>().Count() > 0;
            btNew.Enabled = canCreateMachineType;
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            var machineTypeID = DataConnectorLayer.Instance.R_MachineType.FindOne().MachineTypeID;

            var item = new Machine() { Name = Helper.EMPTY_STRING, MachineTypeID = machineTypeID };
            DataConnectorLayer.Instance.R_Machine.Create(item);
            DataConnectorLayer.Instance.R_Machine.SaveChanges();
            LoadMachineList();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_Machine.FindById(selectedID);
            item.Name = tbNameText.Text != "" ? tbNameText.Text : Helper.EMPTY_STRING;
            item.MachineTypeID = (int)cbMachineType.SelectedValue;
            DataConnectorLayer.Instance.R_Machine.Update(item);
            DataConnectorLayer.Instance.R_Machine.SaveChanges();

            LoadMachineList();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_Machine.FindById(selectedID);
            DataConnectorLayer.Instance.R_Machine.Delete(item);
            DataConnectorLayer.Instance.R_Machine.SaveChanges();

            LoadMachineList();
        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {
            var search = tbFilter.Text.ToLower();

            bindingList.ApplyFilter(delegate(V_MachineList machineList)
            {
                return machineList.Name.ToLower().Contains(search) ||
                       machineList.Identification.ToLower().Contains(search);
            });
        }

        #endregion

    }
}
